// pages/create-pets/create-pets.js
import { Create } from './create-model.js';
import { Config } from '../../utils/config.js';
var create = new Create();
var config = new Config();
Page({

  chooseImage: function () {
    var self = this;

    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      // sourceType: ['album'],
      success: function (res) {
        var imageSrc = res.tempFilePaths[0];

        wx.uploadFile({
          url: Config.restUrl + 'file/upload',
          filePath: imageSrc,
          name: 'file',
          success: function (res) {
            var res = JSON.parse(res.data);
            if (res.code) {
              self.setData({
                imageSrc: res.file
              })
            } else {
              wx.showToast({
                title: res.error,
                icon: 'none',
                duration: 1000
              })
            }
          },
          fail: function ({ errMsg }) {
            wx.showToast({
              title: errMsg,
              icon: 'none',
              duration: 2000
            })
          }
        })

      },
      fail: function ({ errMsg }) {
        console.log('chooseImage fail, err is', errMsg)
      }
    })
  },

  /**
   * 页面的初始数据
   */
  data: {
    petsType: [
      { value: '0', name: '猫派', checked: 'true' },
      { value: '1', name: '狗派' },
      { value: '2', name: '其他'}
    ],
    index: 1,
    date: '2018-01-01',
    jueyu: [
      { value: '1', name: '是', checked: 'true' },
      { value: '0', name: '否' }
    ],
    sex: ['男', '女']
  },

  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },

  formSubmit: function (e) {
    console.log(e.detail.value)
    return;
    if (!e.detail.value.avatar) {
      wx.showToast({
        title: '请上传宠物照片',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (!e.detail.value.name) {
      wx.showToast({
        title: '请输入宠物名字',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (e.detail.value.name.length > 5) {
      wx.showToast({
        title: '宠物名字最多5个字',
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    if (!e.detail.value.sex) {
      wx.showToast({
        title: '请选择性别',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (e.detail.value.info.length > 100) {
      wx.showToast({
        title: '介绍不能超过100字',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    create.createPets(e.detail.value, (data) => {
      if (data.code) {
        wx.showToast({
          title: '创建成功',
          icon: 'success',
          duration: 1000
        })
        setTimeout(function () {
          wx.navigateBack();
        }, 1000)

      } else {
        wx.showToast({
          title: data.error,
          icon: 'none',
          duration: 1000
        })
      }
    });
  }, 

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

})